import { apiClient } from './axios';

// 表单字段模板管理相关接口
export const formTemplatesApi = {
  // 获取指定类型的表单模板
  getFormTemplatesByType: (formType: string) => {
    return apiClient.get(`/form-templates/${formType}`);
  },

  // 获取所有表单类型
  getFormTypes: () => {
    return apiClient.get('/form-templates/types');
  },

  // 获取字段类型列表
  getFieldTypes: () => {
    return apiClient.get('/form-templates/field-types');
  },

  // 获取所有字段模板
  getFormFieldTemplates: (params?: {
    category?: string;
    field_type?: string;
    is_active?: boolean;
  }) => {
    return apiClient.get('/form-templates/fields', { params });
  },

  // 获取表单模板管理列表
  getFormTemplatesManage: (params?: {
    page?: number;
    per_page?: number;
    form_type?: string;
    search?: string;
  }) => {
    return apiClient.get('/form-templates/manage', { params });
  },

  // 创建表单字段模板
  createFormTemplate: (templateData: {
    form_type: string;
    field_name: string;
    field_label: string;
    field_type: string;
    field_options?: any;
    validation_rules?: any;
    default_value?: string;
    placeholder?: string;
    help_text?: string;
    sort_order?: number;
    is_required?: boolean;
    is_active?: boolean;
  }) => {
    return apiClient.post('/form-templates', templateData);
  },

  // 更新表单字段模板
  updateFormTemplate: (templateId: number, templateData: {
    field_label?: string;
    field_type?: string;
    field_options?: any;
    validation_rules?: any;
    default_value?: string;
    placeholder?: string;
    help_text?: string;
    sort_order?: number;
    is_required?: boolean;
    is_active?: boolean;
  }) => {
    return apiClient.put(`/form-templates/${templateId}`, templateData);
  },

  // 删除表单字段模板
  deleteFormTemplate: (templateId: number) => {
    return apiClient.delete(`/form-templates/${templateId}`);
  },

  // 批量更新表单模板
  batchUpdateFormTemplates: (data: {
    ids: number[];
    action: 'activate' | 'deactivate' | 'delete';
  }) => {
    return apiClient.post('/form-templates/batch', data);
  },

  // 复制表单模板
  copyFormTemplate: (data: {
    source_id: number;
    new_form_type: string;
    new_field_name: string;
  }) => {
    return apiClient.post('/form-templates/copy', data);
  },

  // 创建字段模板
  createFormFieldTemplate: (templateData: any) => {
    return apiClient.post('/form-templates/fields', templateData);
  },

  // 更新字段模板
  updateFormFieldTemplate: (templateId: number, templateData: any) => {
    return apiClient.put(`/form-templates/fields/${templateId}`, templateData);
  },

  // 删除字段模板
  deleteFormFieldTemplate: (templateId: number) => {
    return apiClient.delete(`/form-templates/fields/${templateId}`);
  },

  // 预览字段
  previewField: (field: any) => {
    return apiClient.post('/form-templates/preview', field);
  },

  // 验证字段配置
  validateFieldConfig: (config: any) => {
    return apiClient.post('/form-templates/validate', config);
  },

  // 更新字段排序
  updateFieldsOrder: (updates: Array<{id: number, sort_order: number}>) => {
    return apiClient.post('/form-templates/fields/order', { updates });
  },
};